package cn.mymall.web.controller.houtai.mall;

import cn.mymall.entity.mall.Brand;
import cn.mymall.entity.mall.Category;
import cn.mymall.service.mall.IBrandService;
import cn.mymall.service.mall.ICategoryService;
import cn.mymall.service.mall.ISubCategoryService;
import cn.mymall.support.ResultCode;
import cn.mymall.util.BaseWebUtil;
import cn.mymall.util.BindUtils;
import cn.mymall.web.controller.houtai.BaseHoutaiController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
public class BrandApiController extends BaseHoutaiController {
	@Autowired
	private IBrandService brandService;
	@Autowired
	private ICategoryService categoryService;
	@Autowired
	private ISubCategoryService subCategoryService;

	// 分类列表
	@RequestMapping("/api/brand/list")
	public ResultCode<Map> bandList(Integer pagenum, Integer pagesize, Integer brandId , Integer categoryId , Integer sortNum , String up , String below , String name , ModelMap model) {
		if(pagenum==null){
			pagenum = 1;
		}
		if(pagesize==null){
			pagesize = 10;
		}
		List<Category> categoryList = categoryService.list(new QueryWrapper<Category>());

		QueryWrapper<Brand> ew = new QueryWrapper<Brand>();
		if(StringUtils.isNotBlank(name)){
			ew.like("name", name);
		}
		if(sortNum != null){
			ew.eq("sort_num", sortNum);
		}
		if(brandId != null){
			ew.eq("id", brandId);
		}
		if(categoryId != null){
			ew.eq("category_id", categoryId);
		}
		if(sortNum != null){
			ew.eq("sort_num", sortNum);
		}
		ew.orderByAsc("id");
		Page pageParams = new Page(pagenum,pagesize);
		Page<Brand> page = brandService.page(pageParams,ew);
		List<Brand> list = page.getRecords();


		List<Map> resMapList = new ArrayList();
		Map<Integer,Category> categoryMap = Maps.newHashMap();
		for(Brand brand : list){
			if(StringUtils.isNotBlank(up)){
				brand.setSortNum(1);
			}
			if(StringUtils.isNotBlank(below)){
				brand.setSortNum(0);
			}
			Integer curBrandCategoryId = brand.getCategoryId();
			Category category = null;
			String categoryName = null;
			if(curBrandCategoryId != null) {
				if (categoryMap.containsKey(curBrandCategoryId)) {
					category = categoryMap.get(curBrandCategoryId);
				} else {
					category = categoryService.getById(curBrandCategoryId);
					categoryMap.put(curBrandCategoryId, category);
				}
			}
			if(category != null) {
				categoryName = category.getName();
			}

			Map map = new HashMap();
			map.put("id", brand.getId());
			map.put("name", brand.getName());
			map.put("categoryId", brand.getCategoryId());
			map.put("categoryName", categoryName);
			map.put("sortNum", brand.getSortNum());
			map.put("logo", brand.getLogo());
			resMapList.add(map);
		}
		Map map = new HashMap<>();
		map.put("brandList", resMapList);
		map.put("page", ImmutableMap.of("total", page.getTotal(), "pagenum", page.getCurrent()));
		return ResultCode.getSuccessReturn(map);
	}

	//分类联动品牌
	@RequestMapping("/api/brand/listByCategoryId")
	public ResultCode<List<Brand>> listByCategoryId(Integer categoryId, ModelMap model) {
		List<Brand> list = Lists.newArrayList(brandService.list(new QueryWrapper<Brand>().eq("category_id", categoryId)));
		Collections.sort(list, new PropertyComparator<Brand>("id", false, true));
		return ResultCode.getSuccessReturn(list);
	}
	// 分类新增or修改
	@RequestMapping("/api/brand/get")
	public ResultCode<Map> modifyCategory(Integer id) {
		Brand brand = new Brand();
		if (id != null) {
			brand = brandService.getById(id);
		}
		List<Brand> brandList = brandService.list(new QueryWrapper<Brand>());
		List<Category> categoryList = categoryService.list(new QueryWrapper<Category>().gt("sort_Num", 0));

		Map map = new HashMap();
		map.put("obj", brand);
		map.put("brandList", brandList);
		map.put("categoryList", categoryList);
		return ResultCode.getSuccessReturn(map);
	}

	// 分类保存
	@RequestMapping("/api/brand/save")
	public ResultCode saveCategory(Integer id , String name , Integer categoryId, Integer sortNum , String logo) {

		if (sortNum == null) {
			return ResultCode.getFailure("请输入排序");
		}
		if (StringUtils.isBlank(logo)) {
			return ResultCode.getFailure("请选择logo");
		}
		if (StringUtils.isBlank(name)) {
			return ResultCode.getFailure("请输入名称");
		}
		if (categoryId == null) {
			return ResultCode.getFailure("请选择商品类型");
		}
		if (sortNum == null) {
			return ResultCode.getFailure("请输入排序");
		}

		Brand brand = null;
		if (id != null) {
			brand = brandService.getById(id);
		} else {
			brand = new Brand();
		}
		BindUtils.bindData(brand, BaseWebUtil.getRequestParams());
		brandService.saveOrUpdate(brand);
		return ResultCode.SUCCESS;
	}

}
